package com.music.controller;

import cn.hutool.core.bean.BeanUtil;
import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Controller;
import com.laoxuai.ibatis.util.PageInfo;
import com.laoxuai.web.mvc.annotation.RequestMapping;
import com.laoxuai.web.mvc.annotation.ResponseBody;
import com.music.pojo.Music;
import com.music.pojo.Playlist;
import com.music.pojo.bo.PlaylistBO;
import com.music.service.PlaylistService;
import com.music.util.Result;

import java.util.List;

@Controller
@RequestMapping("/playlist")
public class PlaylistController {

    @Autowired
    private PlaylistService playlistServiceImpl;

    /***
     * 新增
     * @param playlistBO
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(PlaylistBO playlistBO) {
        Playlist playlist = new Playlist();
        BeanUtil.copyProperties(playlistBO, playlist);
        System.out.println(playlistBO.getTagStr());
        int result = playlistServiceImpl.insert(playlist, playlistBO.getTagStr());
        if (result == 0) {
            return Result.failed("添加失败", result);
        }
        return Result.success("添加成功", result);
    }

    /***
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(Long id) {
        int result = playlistServiceImpl.delete(id);
        return Result.success("删除成功", result);
    }

    /**
     * 修改
     *
     * @param playlist
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result update(Playlist playlist) {
        int result = playlistServiceImpl.update(playlist);
        if (result == 0) {
            return Result.failed("修改失败", result);
        }
        return Result.success("修改成功", result);
    }

    /***
     * 主键查询
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public Result getByPrimaryKey(Long id) {
        Playlist playlist = playlistServiceImpl.getByPrimaryKey(id);
        if (playlist == null) {
            return Result.failed("查询失败");
        }
        return Result.success("查询成功", playlist);
    }

    /**
     * 分页查询
     *
     * @param playlist
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/page")
    @ResponseBody
    public Result page(Playlist playlist, int page, int size) {
        PageInfo<Playlist> pageInfo = playlistServiceImpl.listPageInfo(playlist, page, size);
        return Result.success("查询成功", pageInfo);
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Result list(Playlist playlist, int page, int size) {
        List<Playlist> list = playlistServiceImpl.listPage(playlist, page, size);
        return Result.success("查询成功", list);
    }

    /**
     * 通过歌单id查找歌曲
     * @param id
     * @return
     */
    @RequestMapping(value = "/pageById")
    @ResponseBody
    public Result pageById(Long id) {
        List<Music> list = playlistServiceImpl.pageById(id);
        if (list == null) {
            return Result.failed("查询失败");
        }
        return Result.success("查询成功", list);
    }

    /**
     * 将歌单id和歌曲id添加到歌单歌曲表中
     * @param playlistId
     * @param id
     * @return
     */
    @RequestMapping(value = "/insertMusicToPlayList")
    @ResponseBody
    public Result insertMusicToPlayList(Long playlistId,Integer id){
        int result = playlistServiceImpl.insertMusicToPlayList(playlistId,id);
        if (result == 0) {
            return Result.failed("修改失败", result);
        }
        return Result.success("修改成功", result);
    }
}